package com.kkwrite.regimen.provider.stream.service;

import com.kkwrite.regimen.provider.exception.ServiceException;
import com.kkwrite.regimen.provider.service.income.IncomeService;
import com.kkwrite.regimen.provider.stream.StreamInputAndOutput;
import com.kkwrite.regimen.provider.stream.dto.IncomeRecordDTO;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * Created by Ron on 2018/9/10 20:16.
 *
 * @author Ron
 */
@Service
public class IncomeRecordService {

    @Resource
    private StreamInputAndOutput streamInputAndOutput;
    @Resource
    private IncomeService incomeService;

    public void send(IncomeRecordDTO record) {
        Assert.notNull(record, "入参不能为null");
        if (!streamInputAndOutput.incomeRecordOutput().send(MessageBuilder.withPayload(record).build())) {
            throw new ServiceException("发送收益记录消息异常！");
        }
    }

    @StreamListener(StreamInputAndOutput.INCOME_RECORD_IN_CHANNEL)
    public void handler(IncomeRecordDTO record) {
        incomeService.incomeRecordMessageHandler(record.getUserId(), record.getOrderCode(),
                record.getAmount(), record.getShopId());
    }

}
